package cn.edu.cimv.controller;

import cn.edu.cimv.constant.InfoConstant;
import cn.edu.cimv.dao.entity.Option;
import cn.edu.cimv.dao.entity.Paper;
import cn.edu.cimv.pojo.Message;
import cn.edu.cimv.pojo.PaperCallBack;
import cn.edu.cimv.service.OptionService;
import cn.edu.cimv.service.PaperService;
import cn.edu.cimv.service.UploadService;
import cn.edu.cimv.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by 门铎 on 2016/8/2.
 */
@Controller
@RequestMapping("/cimv/paper")
public class PaperController {
    @Autowired
    private PaperService paperService;
    @Autowired
    private Message message;
    @Autowired
    private UploadService uploadService;
    @RequestMapping("list/{flag}")
    public String list(Model model,@PathVariable("flag")int paperFlag ){
        model.addAttribute("options",optionService.queryTitles("paper"));
        model.addAttribute("flag",paperFlag);
        model.addAttribute("paperList",paperService.queryPaper(paperFlag));
        return "paper/list";
    }
    @RequestMapping("new/{flag}")
    public String paperNew(Model model,@PathVariable("flag")int paperFlag){
        model.addAttribute("options",optionService.queryTitles("paper"));
        model.addAttribute("flag",paperFlag);
        return "paper/new";
    }

    @RequestMapping( value = "create", method = RequestMethod.POST)
    public @ResponseBody Message create(@ModelAttribute Paper paper) {
        paperService.editPaper(paper);
        message.setMsg(InfoConstant.CREATE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
    public @ResponseBody Message delete(@PathVariable("id")int id){
        paperService.deletePaper(id);
        message.setMsg(InfoConstant.DELETE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updatePage(@PathVariable("id")int id,Model model){
        model.addAttribute("options",optionService.queryTitles("paper"));
        model.addAttribute("paper",paperService.queryPaperById(id));
        return "paper/update";
    }
    @RequestMapping(value = "updatePaper/{id}",method = RequestMethod.POST)
    public @ResponseBody Message updateNews(@PathVariable("id")int id,
                                            @ModelAttribute Paper paper){
        //修改的内容
        paper.setId(id);
        //更新
        paperService.updatePaper(paper);

        message.setMsg(InfoConstant.UPDATE_NORMAL_SUCCESS);
        return message;

    }

    @RequestMapping(value = "pdf",method = RequestMethod.POST)
    public @ResponseBody PaperCallBack pdf(MultipartFile pdf, HttpServletRequest request){
        //上传图片
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        PaperCallBack paperCallBack = uploadService.uploadPdf(pdf,rootPath);
        return paperCallBack;

    }

    /**
     * 分类管理
     */

    @Autowired private OptionService optionService;
    @RequestMapping(value = "direction/list",method = RequestMethod.GET)
    public String direction(Model model){
        model.addAttribute("options",optionService.queryTitles("paper"));
        return "paper/direction";
    }
    @RequestMapping(value = "direction/new",method = RequestMethod.GET)
    public String directionNew(Model model){
        return "paper/direction-new";
    }
    @RequestMapping(value = "direction/update/{id}",method = RequestMethod.GET)
    public  String update(Model model, @PathVariable("id")int id){
        Option option = optionService.queryTitlesById(id);
        model.addAttribute("option",option);

        return "paper/direction-update";
    }
    @RequestMapping(value = "direction/create",method = RequestMethod.POST)
    public @ResponseBody Message create(Model model, Option option){
        option.setCataType("paper");
        optionService.insertOption(option);
        message.setMsg(InfoConstant.CREATE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "direction/updateDirection",method = RequestMethod.POST)
    public @ResponseBody Message updateDirection(Model model, Option option){
        optionService.updateOption(option);
        message.setMsg(InfoConstant.UPDATE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "direction/delete/{id}",method = RequestMethod.GET)
    public @ResponseBody Message delete(Model model,@PathVariable("id")int id){
        optionService.deleteOption(id);
        message.setMsg(InfoConstant.DELETE_NORMAL_SUCCESS);
        return message;
    }
}
